package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //执行分页查询
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.like(name != null, Setmeal::getName, name);
        lqw.orderByDesc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo, lqw);
        //不显示分类,创建dto分页对象,复制分页数据
        Page<SetmealDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, dtoPage, "records");
        //处理分页数据
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> dtoList = new ArrayList<>();
        //遍历分页数据,复制到dto分页
        records.forEach(setmeal -> {
            SetmealDto dto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, dto);
            Category category = categoryService.getById(setmeal.getCategoryId());
            if (category != null) {
                dto.setCategoryName(category.getName());
            }
            dtoList.add(dto);
        });
        dtoPage.setRecords(dtoList);
        return R.success(dtoPage);
    }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
    @CacheEvict(value = "setmealCache",allEntries = true)
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("需要删除的ids{}", ids);
        setmealService.removeWithDish(ids);
        return R.success("套餐删除成功");
    }

    /**
     * 根据条件查询套餐数据
     * @param setmeal
     * @return
     */
    @Cacheable(value = "setmealCache",key = "#setmeal.categoryId+'_'+#setmeal.status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Setmeal::getCategoryId, setmeal.getCategoryId()).eq(Setmeal::getStatus, setmeal.getStatus());
        lqw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }

    /**
     * 修改套餐状态
     * @param s
     * @param ids
     * @return
     */
    @PostMapping("/status/{s}")
    @CacheEvict(value ="setmealCache",allEntries = true)
    public R<String> status(@PathVariable String s,@RequestParam List<Long> ids){
        log.info("需要修改状态:1为启售,0为禁售;{}",s);
        log.info("接收到的id{}",ids);
        //修改套餐状态;接收1,改为启售状态status=1,接收0改为禁售状态status=0
        LambdaUpdateWrapper<Setmeal> lqw=new LambdaUpdateWrapper<>();
        //查找需要修改的id
        lqw.in(Setmeal::getId,ids);
        if ("1".equals(s)){
            //启售update setmeal set status=1
            lqw.set(Setmeal::getStatus,"1");
            setmealService.update(lqw);
        }else{
            //禁售update setmeal set status=0
            lqw.set(Setmeal::getStatus,"0");
            setmealService.update(lqw);
        }
        return R.success("状态修改成功");
    }

    /**
     * 获取当前套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Setmeal> getOne(@PathVariable Long id){
        Setmeal setmeal = setmealService.getByIdWithDish(id);
        return R.success(setmeal);
    }

    /**
     * 修改套餐
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
            log.info("接收到的dto{}",setmealDto);
            setmealService.update(setmealDto);
            return R.success("修改成功");
    }
}
